SciPy is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. It adds significant power to the interactive Python session by providing the user with high-level commands and classes for manipulating and visualizing data. With SciPy an interactive Python session becomes a data-processing and system-prototyping environment rivaling sytems such as MATLAB, IDL, Octave, R-Lab, and SciLab.
In [1]:
from scipy import linalg, optimize
In [2]:
import scipy as sp
sp.info(optimize.fmin)
In [3]:
import numpy as np
from scipy import linalg
arr = np.array([[1, 2], # square matrix
[3, 4]])
print (linalg.det(arr)) # determinant
print (linalg.inv(arr)) # inversion
In [4]:
arr = np.array([[3, 2], # singluar matrix
[6, 4]])
print (linalg.det(arr))
In [5]:
print (linalg.inv(arr)) # inversion of singular matrix
In [6]:
import numpy as np
from scipy import optimize
%matplotlib inline
import matplotlib.pyplot as plt
In [7]:
def f(x):
return x**2 + 10*np.sin(x)
In [8]:
grid = (-10, 10, 0.1)
xmin_global = optimize.brute(f, (grid,))
xmin_local = optimize.fminbound(f, 0, 10)
root = optimize.fsolve(f, 1) # our initial guess is 1
root2 = optimize.fsolve(f, -2.5)
print (xmin_global, xmin_local, root, root2)
In [9]:
xdata = np.linspace(-10, 10, num=50)
np.random.seed(1234)
ydata = f(xdata) + np.random.randn(xdata.size) * 1
In [10]:
def f2(x, a, b):
return a*x**2 + b*np.sin(x)
In [11]:
guess = [.1, .1]
[a, b], params_covariance = optimize.curve_fit(f2, xdata, ydata, guess)
print (a, b)
In [12]:
x = np.arange(-10, 10, 0.1)
plt.plot(x, f(x), 'b-', label="f(x)")
plt.plot(xdata, ydata, '.', label="Synthetic data")
plt.plot(x, f2(x, a, b), 'r--', label="Curve fit result")
xmins = np.array([xmin_global[0], xmin_local])
plt.plot(xmins, f(xmins), 'go', label="Minima")
roots = np.array([root, root2])
plt.plot(roots, f(roots), 'kv', label="Roots", ms=15)
plt.legend()
plt.xlabel('x')
plt.ylabel('f(x)')
Out[12]:
In [13]:
measured_time = np.linspace(0, 1, 10)
noise = (np.random.random(10)*2 - 1) * .3
measures = np.sin(2 * np.pi * measured_time) + noise
In [14]:
from scipy.interpolate import interp1d
computed_time = np.linspace(0, 1, 50) # X - values
linear_interp = interp1d(measured_time,
measures) # create interpolator
linear_results = linear_interp(computed_time) # use interpolator
cubic_interp = interp1d(measured_time,
measures,
kind='cubic') # create interpolator
cubic_results = cubic_interp(computed_time) # use interpolator
In [15]:
plt.plot(measured_time, measures, 'o', ms=6, label='measures')
plt.plot(computed_time, linear_results, '.-', label='linear interp')
plt.plot(computed_time, cubic_results, '.-', label='cubic interp')
plt.legend()
plt.show()
In [16]:
from scipy import ndimage
from scipy import misc
In [17]:
face = misc.face()[:,:,0]
plt.imshow(face, cmap='gray')
plt.show()
In [18]:
noisy_face = face + face.std()*0.5*np.random.standard_normal(face.shape)
plt.imshow(noisy_face, cmap='gray')
Out[18]:
In [19]:
blurred_face = ndimage.gaussian_filter(noisy_face, sigma=3)
plt.imshow(blurred_face, cmap='gray')
Out[19]:
In [20]:
median_face = ndimage.median_filter(noisy_face, size=5)
plt.imshow(median_face, cmap='gray')
Out[20]:
In [21]:
from scipy import signal
wiener_face = signal.wiener(noisy_face, (5,5))
plt.imshow(wiener_face, cmap='gray')
plt.axis('off')
Out[21]:
In [22]:
x, y = np.indices((100, 100))
sig = np.sin(2*np.pi*x/50.)*np.sin(2*np.pi*y/50.)*(1+x*y/50.**2)**2
plt.imshow(sig);plt.colorbar()
Out[22]:
In [23]:
mask = sig > 1
plt.imshow(mask.astype(int));plt.colorbar()
Out[23]:
In [24]:
labels, nb = ndimage.label(mask)
plt.imshow(labels); plt.colorbar()
Out[24]:
In [25]:
label_ids = np.unique(labels)
print (label_ids)
print (ndimage.sum(mask, labels, label_ids))
In [26]:
print (ndimage.maximum(sig, labels, range(1, labels.max()+1)))
In [27]:
sl = ndimage.find_objects(labels==4)
plt.imshow(sig[sl[0]])
Out[27]:
x = np.linspace(-10, 10, 20)
y = 2 * x ** 2 + np.random.randn(x.size) * 10
from scipy import misc
lena = misc.ascent()
In [ ]: